package main

import (
	"context"
	"fmt"
	"time"
)

func f1(c chan struct{}) {

	time.Sleep(time.Second * 1)
	c <- struct{}{}
}
func f2(c chan struct{}) {
	time.Sleep(time.Second * 3)
	c <- struct{}{}
}

func main() {

	c1 := make(chan struct{})
	c2 := make(chan struct{})
	ctx, _ := context.WithTimeout(context.Background(), time.Second*2)

	go func() {
		go f1(c1)
		select {
		case <-ctx.Done():
			fmt.Println("超时")
			break
		case <-c1:
			fmt.Println("c1 done")

		}
	}()

	go func() {
		go f2(c2)
		select {
		case <-ctx.Done():
			fmt.Println("超时")
			break
		case <-c2:
			fmt.Println("c2 done")
		}
	}()

	time.Sleep(time.Second * 5)

}
